﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebsiteDataModel;

public partial class morning_editGgrq : System.Web.UI.Page
{ 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int ggid = 0;
            int deid = 0;
            string txgid = String.IsNullOrEmpty(Request["xgid"]) ? "" : Request["xgid"];
            string tid = String.IsNullOrEmpty(Request["id"]) ? "" : Request["id"];
            if (!int.TryParse(txgid, out ggid))
            {
                ggid = 0;
                this.xgid.Value = "";
            }
            else
            {                
                this.xgid.Value = txgid;
            }

            if (!int.TryParse(tid, out deid))
            {
                deid = 0;
                this.id.Value = "";
            }
            else
            {                
                this.id.Value = tid;
                showInfo();
            }
        }

        if (string.IsNullOrEmpty(this.xgid.Value)) 
        {
            this.ClientScript.RegisterStartupScript(GetType(),"initerror", "<script>alert('必须有相对应的个股才能编辑.');CloesPage();</script>");
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        saveInfo();
    }

    private void showInfo() 
    {
        WebsiteDataEntities de = new WebsiteDataEntities();
        int did = int.Parse(this.id.Value);
        var re = de.XG_ggrq.Where(a => a.id == did);
        if (re.Count() > 0) 
        {
            XG_ggrq node = re.ToArray()[0];
            this.ggrq.Text = node.ggrq;
            this.fxap.Text = node.fxap;
        }
    }

    private void saveInfo() 
    {
        WebsiteDataModel.WebsiteDataEntities de = new WebsiteDataModel.WebsiteDataEntities();
        XG_ggrq node = new XG_ggrq();
        int flag = 1;//1为新增，2为修改
        int did = 0;
        int.TryParse(this.id.Value, out did);
        if (did > 0) 
        {
            var re = de.XG_ggrq.Where(a => a.id == did);
            if (re.Count() > 0) 
            {
                flag = 2;
                node = re.ToArray()[0];
            }
        }

        node.xgid = int.Parse(this.xgid.Value);
        node.ggrq = this.ggrq.Text;
        node.fxap = this.fxap.Text;

        if (flag == 1)
        {
            de.XG_ggrq.AddObject(node);
        }

        int num = de.SaveChanges();
        if (num < 1)
        {
            this.ClientScript.RegisterStartupScript(GetType(), "initerror", "<script>alert('更新失败.');</script>");
        }
        else 
        {
            this.ClientScript.RegisterStartupScript(GetType(), "initerror", "<script>alert('更新成功.');CloesPageReload();</script>");
        }
    }
}